import Model from "./Model";
import WxUtil from "./WxUtil";
import Core from "./Core";

const { ccclass, property } = cc._decorator;
@ccclass
export default class BottomUI extends cc.Component {

    @property(cc.Node)
    tipNode: cc.Node = null;

    @property(cc.Label)
    tipLabel: cc.Label= null;

    clickShare: boolean = false;
    clickShareTime: number = 0;
    onLoad() {
        Core.dispatcher.add('wxVideoEvent', this.onVideoEvent, this)
    }
    

    start() {
        this.tipLabel.string = Model.game.tipCount + ''
        this.tipNode.active = Model.game.tipCount > 0;
        // WxUtil.onShow(this.onWXShow.bind(this))

    }
    onShareClick() {
        this.clickShare = true
        this.clickShareTime = new Date().getTime()
        WxUtil.share()
    }
    onWXShow() {
        if (this.clickShare) {
            var now = new Date().getTime()
            if (now - this.clickShareTime >= 3000)//停留在微信界面3秒
            {
                Model.game.tipCount++;
                this.tipNode.active = Model.game.tipCount > 0;
                this.tipLabel.string = Model.game.tipCount + ''
            }

            this.clickShare = false;
        }

    }
    onReplayClick() {
        if (Model.game.selectedModel == 1)
            cc.director.loadScene('game')
        else if (Model.game.selectedModel == 2)
            cc.director.loadScene('gameDrop')
        else if (Model.game.selectedModel == 3)
            cc.director.loadScene('gameDrag')
        else if (Model.game.selectedModel == 4)
            cc.director.loadScene('gameDraw')
    }
    onTipClick() {
        if (Model.game.tipCount > 0) {
            Model.game.tipCount--;
            this.tipNode.active = Model.game.tipCount > 0;
            this.tipLabel.string = Model.game.tipCount + ''
            this.node.emit('showTip');
        } else {
            //WxUtil.showTip('暂无视频可用,分享好友也可以增加提示哦!')
            WxUtil.showVideo()
        }

    }
    onVideoEvent(v) {
        if (v == 1) {
            Model.game.tipCount++;
            this.tipNode.active = Model.game.tipCount > 0;
            this.tipLabel.string = Model.game.tipCount + ''
        }
    }
    onDestroy()
    {
        Core.dispatcher.remove('wxVideoEvent', this.onVideoEvent, this)
    }

    // update (dt) {}
}
